package com.mine.site.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mine.site.dao.UserDao;
import com.mine.site.model.User;
import com.mine.site.model.bsUser;



@SuppressWarnings("unused")
@WebServlet("/bsServlet")
public class bsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ServletContext session;
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("utf-8");
		
		
		HttpSession session = request.getSession();
		
		bsUser u = (bsUser)session.getAttribute("bsloginUser");
		if(u == null){
			response.sendRedirect("bsLogin.jsp");
			return;
		}
		ServletContext ctx = request.getServletContext();

		PrintWriter out =  response.getWriter();
		
		String method = request.getParameter("m");
		if("del".equals(method)){
			this.del(request, response);
		}else if("forEdit".equals(method)){
			this.beforeEdit(request, response);
		}else if("update".equals(method)){
			this.update(request, response);
		}else if("update1".equals(method)){
			this.update1(request, response);
		}else if("save".equals(method)){
			this.save(request, response);
		}else if("byname".equals(method)){
			this.bynamme(request, response);
		}else if("logoutbs".equals(method)){
			this.logoutbs(request, response);
		}
		else{
			this.list(request, response);
		}
	}

	private void logoutbs(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		 request.getSession().removeAttribute("bsloginUser");
		    request.getRequestDispatcher("bsLogin.jsp").forward(request, response);
		
	}

	private void update1(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		bsUser u = wrapUser1(request);
		UserDao dao = new UserDao();
		dao.update1(u);
		response.sendRedirect("bsServlet");
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
	
	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		
		UserDao dao = new UserDao();
		List<User> users = dao.findAll();
		request.setAttribute("userList", users);
		
		request.getRequestDispatcher("bsindex.jsp").forward(request, response);
		}

	
	private void bynamme(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String name = request.getParameter("name");
		
		UserDao dao = new UserDao();
		List<User> users = dao.findByName(name);
		request.setAttribute("userList", users);
		request.getRequestDispatcher("listUser.jsp").forward(request, response);
	}
	
	private void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("utf-8");
		String loginId = request.getParameter("loginId");
		UserDao dao = new UserDao();
		dao.delete(loginId);
		response.sendRedirect("bsServlet");
	}
	
	private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		User u = wrapUser(request);
		String id=UUID.randomUUID().toString().replaceAll("-", ""); 
		u.setLoginId(id);
		UserDao dao = new UserDao();
		dao.save(u);
		response.sendRedirect("bsServlet");
	}
	
	private void beforeEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String loginId = request.getParameter("loginId");
		UserDao dao = new UserDao();
		User u = dao.findById(loginId);
		request.setAttribute("user", u);
		request.getRequestDispatcher("bsEdit.jsp").forward(request, response);

	}
	
	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		User u = wrapUser(request);
		UserDao dao = new UserDao();
		dao.update(u);
		response.sendRedirect("bsServlet");
	}

	private User wrapUser(HttpServletRequest request) {
		User u = new User();
		u.setLoginId(request.getParameter("loginId"));
		u.setName(request.getParameter("Name"));
		u.setLoginPwd(request.getParameter("loginPwd"));
		u.setPhone(request.getParameter("phone"));
		u.setEmail(request.getParameter("email"));
		u.setAddress(request.getParameter("address"));
		u.setMessage(request.getParameter("message"));
		return u;
	}
	private bsUser wrapUser1(HttpServletRequest request) {
		bsUser u = new bsUser();
		u.setLoginId(request.getParameter("loginId"));
		u.setName(request.getParameter("Name"));
		u.setLoginPwd(request.getParameter("loginPwd"));
		return u;
	}
}
